var express = require("express");
var router = express.Router();
var { Comment, Movie } = require("../utils/model");
var moment = require('moment');
var { checkSession } = require("../utils");
// router.get("/test", (req, res) => {
//     res.send("这是 comment 模块的测试 路由 ")
// })

//如果用户名过期就跳转首页
router.get("/index", (req, res) => {
    console.log(req.session);
    checkSession(req, res, (username) => {
        var {
            mid
        } = req.query;
        Movie.findOne({
            id: mid
        }).then(result => {
            res.render("comment", {
                username,
                result
            })
        })
    })
})

router.post("/submit", (req, res) => {
    console.log(req.body)
    var body = req.body  //post参数提取
    var { mid } = req.query //get参数提取
    checkSession(req, res, (username) => {
        Movie.findOne({ id: mid }).then(movie => {
            body.mid = mid;
            body.time = new Date();
            body.username = username;
            body.movie = movie;
            Comment.insertMany(body).then(result => {
                res.end("ok")
            })
        })
    })
})


// 评论列表  分页
// pageSize  10 20 30 
// page 页码
// total 总条数
// totalPage  总页数 
router.get("/mylist", (req, res) => {
    checkSession(req, res, (username) => {
        var { pageSize, page } = req.query;
        page = page * 1 || 1;
        pageSize = pageSize * 1 || 10;
        var total = 0;
        var totalPage = 0;
        Comment.find({}, {}).then(data => {
            total = data.length;
            totalPage = Math.ceil(total / pageSize);

            //后台做条件限制
            page = page < 1 ? 1 : page;
            page = page > totalPage ? totalPage : page;
            //限制输出
            Comment.find({}, {}).skip((page - 1) * pageSize).limit(pageSize).sort({ _id: -1 }).then(result => {
                res.render("mylist", {
                    result,
                    username,
                    moment: moment,
                    total,
                    page,
                    pageSize,
                    totalPage
                })
            })
        })
    })
})
//删除
router.get("/delone", (req, res) => {
    checkSession(req, res, username => {
        Comment.remove({
            _id: req.query.id
        }).then(result => {
            res.json({
                msg: "删除成功",
                code: 200,
                type: 1,
                result
            })
        })
    })
})
//修改
router.post("/updateone", (req, res) => {
    checkSession(req, res, (username) => {
        const body = req.body;
        Comment.updateMany({
            _id: body.id
        }, {
            $set: {
                title: body.title,
                content: body.content,
                time: new Date()
            }
        }).then(result => {
            res.json({
                msg: "修改成功",
                code: 200,
                type: 1,
                result
            })
        })
    })
})
module.exports = router